package com.dmai.aiet.epc.archive.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmai.aiet.epc.archive.base.LearningCategoryEnum;
import com.dmai.aiet.epc.archive.base.PageResult;
import com.dmai.aiet.epc.archive.entity.OfflineClassStudyRecords;
import com.dmai.aiet.epc.archive.mapper.OfflineClassStudyRecordsDao;
import com.dmai.aiet.epc.archive.pojo.vo.LearningAggregateVo;
import com.dmai.aiet.epc.archive.pojo.vo.LearningCertificateItem;
import com.dmai.aiet.epc.archive.service.OfflineClassStudyRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;


@Service
public class OfflineClassStudyRecordsServiceImpl implements OfflineClassStudyRecordsService {

    @Autowired
    private OfflineClassStudyRecordsDao offlineClassStudyRecordsDao;


    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public OfflineClassStudyRecords findById(String id) {
        return offlineClassStudyRecordsDao.findById(id);
    }

    @Override
    public LearningAggregateVo getAggregate(String userid) {
        List<OfflineClassStudyRecords> records = offlineClassStudyRecordsDao.findByUserid(userid);

        LearningAggregateVo vo = new LearningAggregateVo();
        vo.setCourseCount(records.size());

        vo.setCreditHours(BigDecimal.valueOf(records.stream()
                .map(t -> t.getCreditHours())
                .filter(Objects::nonNull) // 过滤掉为 null 的值
                .reduce((a, b) -> a + b).orElse(0.0)).setScale(2, BigDecimal.ROUND_HALF_UP));

        vo.setLearningHours(BigDecimal.ZERO);

        // 考试暂无学时
        vo.setCreditHours(BigDecimal.ZERO);

        return vo;
    }

    @Override
    public List<LearningCertificateItem> getCertificate(String userid) {
        List<OfflineClassStudyRecords> list = offlineClassStudyRecordsDao.findCertificateByUserid(userid);

        return list.stream().map(t -> {
            LearningCertificateItem item = new LearningCertificateItem();
            item.setCode(t.getCertificateCode());
            item.setName(t.getCertificateName());
            item.setUrl(t.getCertificateUrl());
            item.setAcquireDate(DateUtil.format(t.getCertificateIssueTime(), "yyyy年MM月dd日"));
            item.setValidity(t.getCertificateValidity());
            item.setCategory(LearningCategoryEnum.offlineClass.getCode());
            return item;
        }).collect(Collectors.toList());
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<OfflineClassStudyRecords> pageAll(Map<String, Object> params) {
        Page<OfflineClassStudyRecords> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<OfflineClassStudyRecords> list = offlineClassStudyRecordsDao.pageAll(page, params);
        long total = page.getTotal();
        if (total > 0) {

        }
        return PageResult.<OfflineClassStudyRecords>builder().data(list).code(0).count(total).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<OfflineClassStudyRecords> findAll(Map<String, Object> params) {
        return offlineClassStudyRecordsDao.findAll(params);
    }

}
